package  
{
	
	import flash.display.Graphics;
	import net.flashpunk.Entity;
	import net.flashpunk.Graphic;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.FP;
	import net.flashpunk.utils.Data;
	import net.flashpunk.graphics.Spritemap;
	import net.flashpunk.Sfx;
	import net.flashpunk.utils.Draw;
	import net.flashpunk.graphics.Text;
	
	/**
	 * ...
	 * @author Felix Fleiß, Martina Rasch
	 */
	public class Player extends Entity
	{
		//embed image
		[Embed(source = 'res/ufo.png')] 
		private const PLAYER:Class;
		[Embed(source = 'res/crash.mp3')] private const CRASH:Class;
		[Embed(source = 'res/extratime.mp3')] private const TIME:Class;
		
		private var player:Spritemap = new Spritemap(PLAYER, 80, 50);
		
		//CHANGE AMOUNT OF HP HERE!!!
		private  var hp:int = 10000;
		public var singingToNote:SingingToNote;
		private var speed:Number;
		private var horizontalSpeed:Number;
		private var horizontalAcceleration:Number;
		private var horizontalBrake:Number;
		private var obstacleBrakePunishment:Number;
		private var horizontalMaxSpeed:Number;
		private var screen_width:int;
		private var screen_height:int;
		private var player_width:int;
		private var player_height:int;
		private var score:Score;
		private var background:Background;
		private var power:Power;
		public var crash:Sfx = new Sfx(CRASH);
		private var bonusTime:Number;
		public var timeBonus:Sfx = new Sfx(TIME);
		public static var s_x:Number;
		public static var s_y:Number;
		private var singingTolerance:Number;
		private var crashed:Boolean = false;
		private var crashTime:Number = 0;
		
		public function Player(playerScore:Score, playerBackground:Background, playerPower:Power) 
		{
			
			background = playerBackground;
			score = playerScore;
			screen_width = Data.readInt("screen_width");
			screen_height = Data.readInt("screen_height");
			player_width = Data.readInt("player_width");
			player_height = Data.readInt("player_height");
			bonusTime = Number(Data.readString("player_bonustime"));
			singingTolerance = Number(Data.readString("singing_tolerance"));
			
			singingToNote = new SingingToNote();
			
			player.add("fly", [0, 1], 60, true);
			player.add("crash", [2], 60, true);
			
			//draws player on position (x, y)
			graphic = player;
			graphic.x = -40
			graphic.y = -25;
			//set hitbox
			setHitbox(player_width, player_height, player_width / 2, player_height / 2);
			
			speed = Number(Data.readString("player_speed"));
			horizontalSpeed = Number(Data.readString("player_horizontal_speed"));
			horizontalAcceleration = Number(Data.readString("player_horizontal_acceleration"));
			horizontalBrake = Number(Data.readString("player_horizontal_brake"));
			obstacleBrakePunishment = Number(Data.readString("player_horizontal_brake_obstacle_punishment"));
			horizontalMaxSpeed = Number(Data.readString("player_horizontal_max_speed"));
			//x and y are inherited from Entity
			y = screen_height - singingToNote.getOctaveNormedNote() * screen_height;
			x = player_width / 2+20;
			power = playerPower;
			power.setPos( x + 80, y);
		}
		
		//update the player
		override public function update():void 
		{
			if (!crashed) {
				player.play("fly");
			} else {
				player.play("crash");
				crashTime += FP.elapsed;
				if (crashTime > 0.4) {
					crashed = false;
				}
			}
			//player movement
			var dist:int = (2*screen_height - singingToNote.getOctaveNormedNote() * screen_height)%screen_height; 
			if (dist <= 0 || dist >=screen_height)
			trace(dist)
			power.setPos(30, dist);
			x = power.x + 50 + horizontalSpeed * 10;
			//done
			var note:String = singingToNote.getSungNote(singingTolerance);
			//hack to hit note always
			//note = "a";
			//horizontal movement
			if (note != null){//note was hit
				//trace(note);
				horizontalSpeed += horizontalAcceleration * FP.elapsed;
				if (horizontalSpeed > horizontalMaxSpeed)
					horizontalSpeed = horizontalMaxSpeed;
			}else {
				horizontalSpeed -= horizontalBrake * FP.elapsed;
				if (horizontalSpeed < 0)
					horizontalSpeed = 0;
			}
			score.addScore(horizontalSpeed * FP.elapsed);
			background.setSpeed(horizontalSpeed);
			//vertical movement
			var move:Number = FP.elapsed * speed;
			//if (dist < screen_height - 10) {
				if (dist < y)
				{
					if (y - dist >= move)
					{
						y -= move;
					} else {
						y -= y - dist;
					}
				}else {
					if (dist - y >= move)
					{
						y += move;
					} else {
						y += dist - y;
					}
				}
			//}
			
			//collision detection
			// Assign the collided obstacle to a temporary var
			var o:Obstacle = collide("obstacle", x, y) as Obstacle;
			// Check if o has a value (true if a Obstacle was collided with).
			if (o) {
				if (!o.getDead()) {
					crashed = true;
					crashTime = 0;
					player.play("crash");
					crash.play();
					trace("hit obstacle lost speed");
					horizontalSpeed -= obstacleBrakePunishment;
					if (horizontalSpeed < 0)
						horizontalSpeed = 0;
					o.destroy(); 
				}
			}
			
			var t:TimeBonus = collide("timebonus", x, y) as TimeBonus;
			if (t) {
				if (!t.getDead()) {
					score.addTime(bonusTime);
					t.destroy();
					timeBonus.play();
				}
			}
			s_x = x;
			s_y = y - player_height/2;
		}
		override public function render():void 
		{
			//Draw.line(x, y, power.x, y, 0x000099);
			super.render();
		}
	}

}